﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_OperationHandlerInspector
	{
		private class MyBaseResult
		{
		}
		private class MyResult : MyBaseResult
		{
		}
		private class MyOperation : Operation<MyBaseResult, Int32>
		{
		}
		private class MyMessage : Message
		{
		}
		private class MyInformation : Information
		{
		}

		private abstract class TestBaseClass
		{
			public abstract MyBaseResult Method2(MyOperation operation);

			[OperationHandler]
			public abstract MyBaseResult Method3(MyOperation operation);
		}

		private class TestClass : TestBaseClass
		{
			public MyBaseResult Method1(MyOperation operation)
			{
				// no attribute
				return new MyBaseResult();
			}

			[OperationHandler]
			public override MyBaseResult Method2(MyOperation operation)
			{
				// abstract method with attribute
				return new MyBaseResult();
			}

			public override MyBaseResult Method3(MyOperation operation)
			{
				// abstract method with attribute derived
				return new MyBaseResult();
			}

			[OperationHandler]
			public MyBaseResult Method4<T>(MyOperation operation)
			{
				// generic method
				return new MyBaseResult();
			}

			[OperationHandler]
			public MyBaseResult Method5()
			{
				// no parameters
				return new MyBaseResult();
			}

			[OperationHandler]
			public MyBaseResult Method6(MyOperation operation, Object dummy)
			{
				// too many parameters
				return new MyBaseResult();
			}

			[OperationHandler]
			public MyBaseResult Method7(Object operation)
			{
				// invalid parameter type
				return new MyBaseResult();
			}

			[OperationHandler]
			public void Method8(MyOperation operation)
			{
				// invalid return type
			}

			[OperationHandler]
			public MyBaseResult Method9(MyOperation operation)
			{
				// exact return type
				return new MyBaseResult();
			}

			[OperationHandler]
			public MyResult Method10(MyOperation operation)
			{
				// assignable return type
				return new MyResult();
			}

			[OperationHandler]
			public None Method11(Operation<None, Int32> operation)
			{
				return None.Instance;
			}

			[OperationHandler]
			public void Method12(Operation operation)
			{
			}

			[OperationHandler]
			public void Method13(MyMessage operation)
			{
			}

			[OperationHandler]
			public void Method14(MyInformation operation)
			{
			}

			[OperationHandler]
			public async Task<MyBaseResult> Method15(MyOperation operation)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return null;
			}

			[OperationHandler]
			public async Task<None> Method16(Operation<None, String> operation)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return null;
			}

			[OperationHandler]
			public async Task Method17(MyOperation operation)
			{
				// must return Task<T>
				await Task.Factory.StartNew(() => Thread.Sleep(10));
			}

			[OperationHandler]
			public async void Method18(MyOperation operation)
			{
				// must return Task<T>
				await Task.Factory.StartNew(() => Thread.Sleep(10));
			}

			[OperationHandler]
			public async Task<Object> Method19(Operation<Object, Int32> operation)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return "DONE";
			}

			[OperationHandler]
			public async Task<String> Method19B(Operation<Object, Int32> operation)
			{
				// Task<Object> is required, although String might be returned
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return "DONE";
			}

			[OperationHandler]
			public async Task<Object> Method19C(Operation<String, Int32> operation)
			{
				// Task<String> is required
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return "DONE";
			}

			[OperationHandler]
			public Task<MyBaseResult> Method20(MyOperation operation)
			{
				return null;
			}

			[OperationHandler]
			public async Task<String> Method21(Operation<None, Int32> operation)
			{
				await Task.Factory.StartNew(() => Thread.Sleep(10));
				return null;
			}
		}


		private MethodInfo GetMethodInfo(String name)
		{
			var target = new TestClass();
			return target.GetType().GetMethod(name);
		}

		private OperationHandlerInfo InspectMethod(String name)
		{
			var methodInfo = GetMethodInfo(name);
			var inspector = new OperationHandlerInspector();
			return (OperationHandlerInfo)inspector.Inspect(methodInfo);
		}


		// --- TESTS ---


		[Test]
		public void Test_Inspect_NoAttribute()
		{
			var handlerInfo = InspectMethod("Method1");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_AbstractMethod_DerivedMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method2");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(MyBaseResult)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_AbstractMethod_BaseMethodWithAttribute()
		{
			var handlerInfo = InspectMethod("Method3");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(MyBaseResult)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_GenericMethod()
		{
			var handlerInfo = InspectMethod("Method4");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_NoParameter()
		{
			var handlerInfo = InspectMethod("Method5");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_TwoParameters()
		{
			var handlerInfo = InspectMethod("Method6");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ParameterNotOperationType()
		{
			var handlerInfo = InspectMethod("Method7");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ReturnTypeVoid()
		{
			var handlerInfo = InspectMethod("Method8");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_ExactReturnType()
		{
			var handlerInfo = InspectMethod("Method9");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(MyBaseResult)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_AssignableReturnType()
		{
			var handlerInfo = InspectMethod("Method10");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(MyBaseResult)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_Operation_GenericBaseType()
		{
			var handlerInfo = InspectMethod("Method11");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.False);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Operation<None, Int32>)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(None)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_Operation_NonGenericBaseType()
		{
			var handlerInfo = InspectMethod("Method12");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_MessageBaseType()
		{
			var handlerInfo = InspectMethod("Method13");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_InformationBaseType()
		{
			var handlerInfo = InspectMethod("Method14");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_Async()
		{
			var handlerInfo = InspectMethod("Method15");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.True);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(MyOperation)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(MyBaseResult)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_Operation_Async_GenericOperationBaseType()
		{
			var handlerInfo = InspectMethod("Method16");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.True);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Operation<None, String>)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(None)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(String)));
		}

		[Test]
		public void Test_Inspect_Operation_Async_NonGenericTaskReturnType()
		{
			var handlerInfo = InspectMethod("Method17");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_Async_VoidReturnType()
		{
			var handlerInfo = InspectMethod("Method18");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_Async_ExcactReturnType()
		{
			var handlerInfo = InspectMethod("Method19");

			Assert.That(handlerInfo, Is.Not.Null);
			Assert.That(handlerInfo.IsAsync, Is.True);
			Assert.That(handlerInfo.MessageType, Is.EqualTo(typeof(Operation<Object, Int32>)));
			Assert.That(handlerInfo.ResultType, Is.EqualTo(typeof(Object)));
			Assert.That(handlerInfo.ProgressType, Is.EqualTo(typeof(Int32)));
		}

		[Test]
		public void Test_Inspect_Operation_Async_AssignableReturnType()
		{
			var handlerInfo = InspectMethod("Method19B");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_Async_NonAssignableReturnType()
		{
			var handlerInfo = InspectMethod("Method19C");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_Async_NonAssignableReturnType2()
		{
			var handlerInfo = InspectMethod("Method21");

			Assert.That(handlerInfo, Is.Null);
		}

		[Test]
		public void Test_Inspect_Operation_GenericTaskReturnType()
		{
			var handlerInfo = InspectMethod("Method20");

			Assert.That(handlerInfo, Is.Null);
		}

	}
}

#endif
